Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mariadb

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mariadb

fast mariadb/mysql connector.

  • 2.5.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
120K
increased by3.54%
Maintainers
3
Weekly downloads
 
Created

What is mariadb?

The mariadb npm package is a Node.js connector for MariaDB, a popular open-source relational database. It allows you to interact with MariaDB databases using JavaScript, providing functionalities for connecting to the database, executing queries, managing transactions, and handling connection pools.

What are mariadb's main functionalities?

Connecting to the Database

This code demonstrates how to create a connection pool and connect to a MariaDB database using the mariadb package. It establishes a connection and logs a message upon successful connection.

const mariadb = require('mariadb');
const pool = mariadb.createPool({
  host: 'localhost', 
  user: 'yourUsername', 
  password: 'yourPassword', 
  database: 'yourDatabase'
});

async function connect() {
  let conn;
  try {
    conn = await pool.getConnection();
    console.log('Connected to the database');
  } catch (err) {
    throw err;
  } finally {
    if (conn) conn.end();
  }
}

connect();

Executing Queries

This code sample shows how to execute a simple SELECT query using the mariadb package. It retrieves all rows from a specified table and logs them to the console.

const mariadb = require('mariadb');
const pool = mariadb.createPool({
  host: 'localhost', 
  user: 'yourUsername', 
  password: 'yourPassword', 
  database: 'yourDatabase'
});

async function executeQuery() {
  let conn;
  try {
    conn = await pool.getConnection();
    const rows = await conn.query('SELECT * FROM yourTable');
    console.log(rows);
  } catch (err) {
    throw err;
  } finally {
    if (conn) conn.end();
  }
}

executeQuery();

Managing Transactions

This code demonstrates how to manage transactions using the mariadb package. It begins a transaction, executes an INSERT query, and commits the transaction. If an error occurs, the transaction is rolled back.

const mariadb = require('mariadb');
const pool = mariadb.createPool({
  host: 'localhost', 
  user: 'yourUsername', 
  password: 'yourPassword', 
  database: 'yourDatabase'
});

async function manageTransaction() {
  let conn;
  try {
    conn = await pool.getConnection();
    await conn.beginTransaction();
    await conn.query('INSERT INTO yourTable (column1, column2) VALUES (?, ?)', [value1, value2]);
    await conn.commit();
    console.log('Transaction committed');
  } catch (err) {
    if (conn) await conn.rollback();
    console.error('Transaction rolled back', err);
  } finally {
    if (conn) conn.end();
  }
}

manageTransaction();

Handling Connection Pools

This code sample shows how to handle connection pools with the mariadb package. It sets a connection limit and demonstrates how to use the pool to execute a query.

const mariadb = require('mariadb');
const pool = mariadb.createPool({
  host: 'localhost', 
  user: 'yourUsername', 
  password: 'yourPassword', 
  database: 'yourDatabase',
  connectionLimit: 5
});

async function usePool() {
  let conn;
  try {
    conn = await pool.getConnection();
    const rows = await conn.query('SELECT * FROM yourTable');
    console.log(rows);
  } catch (err) {
    throw err;
  } finally {
    if (conn) conn.end();
  }
}

usePool();

Other packages similar to mariadb

Keywords

FAQs

Package last updated on 17 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc